package com.zwz.crm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.hash.Hashing;
import com.zwz.crm.dao.UserMapper;
import com.zwz.crm.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaowuz on 2020/1/9.
 */
@Service
public class UserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userMapper;

    public User checkUser(String account,String pwd) {
        pwd = Hashing.sha256().hashBytes(pwd.getBytes()).toString();
        Map<String, String> userMap = new HashMap<>();
        userMap.put("account", account);
        userMap.put("pwd", pwd);
        List<User> users = userMapper.selectList(new QueryWrapper<User>().allEq(userMap));
        if (CollectionUtils.isEmpty(users) && users.size()>1) {
            LOGGER.warn("存在多个用户 {}",users);
        }
        return CollectionUtils.isEmpty(users)? null:users.get(0);
    }

}
